<?php
namespace app\issue\controller;
use app\common\controller\IssueBase;

/*主体内容部分*/
class Main extends IssueBase
{
	public function _initialize(){
    	$filter = array("login", "login_post", "login_out");
    	if (!in_array(request()->action(), $filter)) {
    		parent::_initialize();
    	}
    }

    /**
     * 后台首页
     * @return mixed
     */
	public function index() {
        $auth_group = sp_get_auth_menu();
        $this->assign("menus", $auth_group);
        $this->assign("issue", sp_get_current_issue());
		return $this->fetch();
	}

    /**
     * 登录页面
     */
	public function login(){
		return $this->fetch();
	}

    /**
     * 登录post
     */
	public function login_post(){
        $account = input('account/s');
        $psw     = input('psw/s');
        $code    = input('code/s');
        if (empty($account)) {
            $this->ajax_error("账号不能为空");
        }
        if (empty($psw)) {
            $this->ajax_error("密码不能为空");
        }
        if (empty($code)) {
            $this->ajax_error("验证码不能为空");
        }
        if (!captcha_check($code)) {
            $this->ajax_error("验证码错误");
        }
        $op_users = db("operation_user")->where(array("account" => array("eq", $account), "password" => array("eq", md5($psw))))->find();
        if (empty($op_users)) {
            $this->ajax_error("账号或者密码错误！");
        } else {
            session("Operation", $op_users);
            $this->ajax_success("验证通过！");
        }
	}


	/*退出*/
	public function login_out(){
		session("Issue", null);
        $this->redirect('/Issue/Main/login');
	}


    /**
     * 密码修改信息
     */
    public function editPsw() {
        if(request()->isPost()){
            $old_pwd = input("old_password/s");
            $password = input("password/s");
            if (empty($old_pwd)) {
                $this->error("原密码不能为空");
            }
            if (empty($password)) {
                $this->error("新密码不能为空");
            }
            $operation_id = sp_get_current_operation_id();
            $old_password = db("operation_user")->where(array("id" => array("eq", $operation_id)))->value("password");
            if($old_password != md5($old_pwd)){
                $this->ajax_error("原密码错误");
            }
            $update_res = db("operation_user")->where(array("id" => array("eq", $operation_id)))->setField(array("password" => md5($password)));
            $this->ajax_success("修改成功");
        }else{
            echo $this->fetch();
        }
    }
}
